/**
 * 将字符串模板转换成字符串
 * @param str 字符串模板
 * @param obj 模板对象
 */
export function stringTemplate(str: string, obj?: any): string {
  str = str
    .replace(/\n/g, '')
    .replace(/{{(?!=)/g, "';str+=it.")
    .replace(/{{=/g, "'+")
    .replace(/}}/g, ";str+='");
  let fn = new Function('it', `let str='';str+='${str}';return str;`);
  return fn(obj);
}

export function camelCase(str?: string): string {
  if (!str) {
    return '';
  }
  const arr = str.split('_');
  for (var i = 1; i < arr.length; i++) {
    arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1, arr[i].length - 1);
  }
  return arr.join('');
}

export function snakeCase(str?: string): string {
  if (!str) {
    return '';
  }
  var arr = str.split('');
  for (var i = 1; i < arr.length; i++) {
    if (/[A-Z]/.test(arr[i])) {
      arr[i] = '_' + arr[i].toLowerCase();
    }
  }
  return arr.join('');
}
